layui.use(['jquery','layer','table'],function () {
    var $ = layui.jquery,
        layer = layui.layer,
        table = layui.table;
    var today = "";
    var userType = 1;
    localStorage.setItem("userType",userType);

    //当前登录人
    var user = localStorage.getItem('user');
    user = JSON.parse(user);
    var userId = user.data.userId;

    //刷新页面
    $("#refresh").click(function () {
        location.reload();
    });
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });

    var flag = true;//锁 true代表前端用户 false代表后台管理用户
    //按钮样式
    $('.btn_style').on('click',function () {
        $(this).addClass('btn_onclick').siblings().removeClass('btn_onclick');
        $(this).removeClass('btn_remove').siblings().addClass('btn_remove');
    })

    //评论类型赋值赋值
    Commentcounts();

    //点击前端展示的表格
    $("#users").on('click',function () {
        getTbale();
        flag = true;
        //评论类型赋值赋值
        Commentcounts();
        //给中间表格赋值
        historyComment();
        userType = 1 ;
        localStorage.setItem("userType",userType);
    });
    //点击管理人员展示的表格
    $("#manager").on('click',function () {
        getUserTable();
        flag = false;
        //评论类型赋值赋值
        Commentcounts();
        //给中间表格赋值
        historyComment();
        userType = 2;
        localStorage.setItem("userType",userType);
    });
    //评论类型赋值赋值
    function Commentcounts(){
        if(flag){
            //给前端微圈全部评论赋值
            $.ajax({
                url:"/comment/getPersonalCommentCount",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"userId":userId},
                success:function(data){
                    $("#allCommentNum").text(data.data)
                }
            })
            //get正常赋值
            $.ajax({
                url:"/comment/getPersonalCommentCountByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"status":1,"userId":userId},
                success:function(data){
                    $("#normalCommentNum").text(data.data)
                }
            })
            //已拦截
            $.ajax({
                url:"/comment/getPersonalCommentCountByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"status":2,"userId":userId},
                success:function(data){
                    $("#interceptCommentNum").text(data.data)
                }
            })
            //已删除
            $.ajax({
                url:"/comment/getPersonalCommentCountByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"status":3,"userId":userId},
                success:function(data){
                    $("#deleteCommentNum").text(data.data)
                }
            })
        }else{
            //给后端微圈全部评论赋值
            $.ajax({
                url:"/comment/getPersonalCommentCount",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"userId":userId},
                success:function(data){
                    $("#allCommentNum").text(data.data)
                }
            })
            //正常
            $.ajax({
                url:"/comment/getPersonalCommentCountByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"status":1,"userId":userId},
                success:function(data){
                    console.log(data.data);
                    $("#normalCommentNum").text(data.data)
                }
            })
            //已拦截
            $.ajax({
                url:"/comment/getPersonalCommentCountByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"status":2,"userId":userId},
                success:function(data){
                    console.log(data.data);
                    $("#interceptCommentNum").text(data.data)
                }
            })
            //已删除
            $.ajax({
                url:"/comment/getPersonalCommentCountByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"status":3,"userId":userId},
                success:function(data){
                    console.log(data.data);
                    $("#deleteCommentNum").text(data.data)
                }
            })

        }
    }

    //给历史评论次数赋值
    historyComment();
    function historyComment(){
        getTime();
        if(flag){
            //历史评论次数
            $.ajax({
                url:"/comment/getPersonalCommentCount",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"userId":userId},
                success:function(data){
                    $("#historyCount").text(data.data)
                }
            })
            //历史评论人数
            $.ajax({
                url:"/comment/personalHistoryComment",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"userId":userId},
                success:function(data){
                    $("#historyPeople").text(data.data)
                }
            })
            //今日评论次数
            $.ajax({
                url:"/comment/getCommentUserAndData",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4},
                success:function(data){
                    var todayCount = 0;
                    for (var i = 0; i <data.data.length; i++) {
                        if(data.data[i].commentDate.substring(0,10)==today.substring(0,10)){
                            todayCount++;
                        }
                    }
                    $("#todayCount").text(todayCount);
                }
            })
            //今日评论人数
            $.ajax({
                url:"/comment/getCommentUserAndData",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4},
                success:function(data){
                    var arr = [];
                    for (var i = 0; i <data.data.length; i++) {
                        if(data.data[i].commentDate.substring(0,10)==today.substring(0,10)){//今日
                            arr.push(data.data[i].userId);
                        }
                    }
                    //数组去重
                    var arr1 = [];
                    for(var i=0,len=arr.length;i<len;i++){
                        if(arr1.indexOf(arr[i]) === -1){
                            arr1.push(arr[i]);
                        }
                    }
                    $("#todayPeople").text(arr1.length);
                }
            })
        }else{
            //历史评论次数
            $.ajax({
                url:"/comment/getPersonalCommentCount",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"userId":userId},
                success:function(data){
                    $("#historyCount").text(data.data)
                }
            })
            //历史评论人数
            $.ajax({
                url:"/comment/personalHistoryComment",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"userId":userId},
                success:function(data){
                    $("#historyPeople").text(data.data)
                }
            })
            //今日评论次数
            $.ajax({
                url:"/comment/getCommentUserAndData",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4},
                success:function(data){
                    var todayCount = 0;
                    for (var i = 0; i <data.data.length; i++) {
                        if(data.data[i].commentDate.substring(0,10)==today.substring(0,10)){
                            todayCount++;
                        }
                    }
                    $("#todayCount").text(todayCount);
                }
            })
            //今日评论人数
            $.ajax({
                url:"/comment/getCommentUserAndData",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4},
                success:function(data){
                    var arr = [];
                    for (var i = 0; i <data.data.length; i++) {
                        if(data.data[i].commentDate.substring(0,10)==today.substring(0,10)){//今日
                            arr.push(data.data[i].userId);
                        }
                    }
                    //数组去重
                    var arr1 = [];
                    for(var i=0,len=arr.length;i<len;i++){
                        if(arr1.indexOf(arr[i]) === -1){
                            arr1.push(arr[i]);
                        }
                    }
                    $("#todayPeople").text(arr1.length);
                }
            })

        }
    }

    //点击不同的状态显示不同的数据
    //点击全部评论
    $("#allComment").on('click',function () {
        if(flag){
            getTbale();
        }else{
            getUserTable();
        }
    });

    //点击正常
    $("#normalComment").on('click',function(){
        if(flag){
            $.ajax({
                url:"/comment/getPersonalBeforeMicrocircleCommentByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"status":1,"userId":userId},
                success:function(data){
                    demoTable(data.data);
                }
            })
        }else{
            $.ajax({
                url:"/comment/getPersonalUserMicrocircleCommentByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"status":1,"userId":userId},
                success:function(data){
                    demoTable(data.data);
                }
            })
        }
    });

    //点击已拦截
    $("#interceptComment").on('click',function(){
        if(flag){
            $.ajax({
                url:"/comment/getPersonalBeforeMicrocircleCommentByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"status":2,"userId":userId},
                success:function(data){
                    demoTable(data.data);
                }
            })
        }else{
            $.ajax({
                url:"/comment/getPersonalUserMicrocircleCommentByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"status":2,"userId":userId},
                success:function(data){
                    demoTable(data.data);
                }
            })
        }
    });

    //点击已删除
    $("#deleteComment").on('click',function(){
        if(flag){
            $.ajax({
                url:"/comment/getPersonalBeforeMicrocircleCommentByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":1,"type":4,"status":3,"userId":userId},
                success:function(data){
                    demoTable(data.data);
                }
            })
        }else{
            $.ajax({
                url:"/comment/getPersonalUserMicrocircleCommentByStatus",
                type:"post",
                dataType:"json",
                data:{"userType":2,"type":4,"status":3,"userId":userId},
                success:function(data){
                    demoTable(data.data);
                }
            })
        }
    });


    //数据表格  前端用户评论
    getTbale();
    function getTbale() {
        $.ajax({
            url:"/comment/getPersonalBeforeMicrocircleComment",
            type:"post",
            dataType:"json",
            data:{"userType":1,"type":4,"userId":userId},
            success:function(data){
                demoTable(data.data);
            }
        })
    }

    //后台用户评论
    function getUserTable(){
        $.ajax({
            url:"/comment/getPersonalUserMicrocircleComment",
            type:"post",
            dataType:"json",
            data:{"userType":2 ,"type":4,"userId":userId},
            success:function(data){
                demoTable(data.data);
            }
        })
    }

    function demoTable(data){
        table.render({
            elem: '#demoTable',
            title: '数据列表',
            data: data,
            toolbar: '#button_ids',
            limit: 10,
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                limits:[10,20,30],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox',float:'left'},
                {title:'序号',align:'center',width:100,templet:function (data) {
                        return data.LAY_INDEX;
                    }},
                {field:'account',title:'成员账号',align:'center'},
                {field:'commonName',title:'昵称',align:'center'},
                {field:'artcleTitle',title:'所属文章',align:'center'},
                {field:'commentDate',title:'发言时间',align:'center'},
                {field:'content',title:'发言内容',align:'center'},
                {field:'status',title:'状态',align:'center',templet:function (data) {
                        if(data.status == 1){
                            return "正常";
                        }else if(data.status == 2){
                            return '已拦截';
                        }else if(data.status == 3){
                            return '已删除';
                        }else {
                            return '暂无状态';
                        }
                    }},
                {field:'',title:'操作',align:'center',templet:function (data) {
                        if(data.status == 1){
                            return '<a onclick="javascript:checkTable('+ data.id +');">查看</a>&nbsp;&nbsp;<a onclick="javascript:interceptTable('+ data.id +","+"'"+data.content+"'" +');">拦截</a>&nbsp;&nbsp;<a onclick="javascript:deleteTable('+ data.id +');">删除</a>'
                        }else if(data.status == 2){
                            return '<a onclick="javascript:checkTable('+ data.id +');">查看</a>&nbsp;&nbsp;<a onclick="javascript:recoverTable('+ data.id +"," +"'"+data.content+"'" +');">恢复</a>&nbsp;&nbsp;<a onclick="javascript:deleteTable('+ data.id +');">删除</a>'
                        }else if(data.status == 3){
                            return '<a onclick="javascript:checkTable('+ data.id +');">查看</a>';
                        }else{
                            return '无操作';
                        }
                    }},
            ]]
        })
    }

    //查看评论详情
    checkTable = function checkTable(id){
        $.ajax({
            url: '/comment/getMicrocircleInformationById',
            type: 'post',
            dataType: 'json',
            data: {'id':id},
            success: function (data) {
                localStorage.setItem('commentMicrocircle',JSON.stringify(data.data));
                layer.open({
                    type:2,
                    title:'微圈查看',
                    area:['95%','100%'],
                    scrollbar: false,
                    offset:'auto',
                    btn:["确定","取消"],
                    content:"/comment/microcircleComment/microcircle-list-select.html",
                    success:function (layero, index) {
                        var sonData = layero.find('iframe').contents();//获取子页面内容
                        sonData.find("th[name=artcleTitle]").text(data.data.artcleTitle);//微圈标题
                        sonData.find("th[name=userName]").text(data.data.createName);//创作人
                        sonData.find("th[name=artcleCategroyName]").text(data.data.categroyName);//微圈分类名
                        sonData.find("th[name=releaseDate]").text(data.data.releaseDate);//发布时间
                        sonData.find("th[name=account]").text(data.data.account);//创建人账号
                        sonData.find("#article").val(data.data.article);//文章内容
                        localStorage.setItem("artcleUrl",data.data.artcleUrl);//文章图片路径
                        if(data.data.acticleHits == null){
                            sonData.find("th[name=acticleHits]").text(0);
                        }else{
                            sonData.find("th[name=acticleHits]").text(data.data.acticleHits);//文章点击数量
                        }
                        if(data.data.acticleLike == null){
                            sonData.find("th[name=acticleLike]").text(0);
                        }else{
                            sonData.find("th[name=acticleLike]").text(data.data.acticleLike);//文章点赞数量
                        }
                        if(data.data.acticleLike == null){
                            sonData.find("th[name=acticleShare]").text(0);
                        }else{
                            sonData.find("th[name=acticleShare]").text(data.data.acticleShare);//文章的分享量
                        }
                    },
                    yes:function (index, layero) {
                        layer.close(index);
                    },
                    but2:function (index, layero) {
                        layer.close(index);
                    }
                })
            }
        })

    }

    //拦截评论
    interceptTable = function interceptTable(id, content) {
        layer.confirm('您确定要拦截'+ "‘" + content +"’" + "信息吗？",
            function (index) {
                editComment('2',id);
            })
    }
    //恢复评论
    recoverTable = function recoverTable(id, content) {
        layer.confirm('您确定要恢复'+ "‘" + content +"’" + "信息吗？",
            function (index) {
                editComment('1',id);
            })
    }

    //修改评论
    function editComment(status,id) {
        $.ajax({
            url: '/comment/editComment',
            type: 'post',
            dataType: 'json',
            data: {'status':status,'id':id},
            success: function (data) {
                if(data.data){
                    location.reload();
                }else {
                    layer.msg('操作失败')
                }
            }
        })
    }

    //删除评论
    deleteTable = function deleteTable(id){
        layer.confirm('您确定要删除此条评论吗？',
            function (index) {
                $.ajax({
                    url: '/comment/delComment',
                    type: 'post',
                    dataType: 'json',
                    data: {'status':status,'id':id},
                    success: function (data) {
                        if(data.data){
                            layer.msg('删除成功')
                            if(flag){
                                getTbale();
                                Commentcounts()//不同按钮赋值
                                historyComment();//不同历史评论赋值
                            }else{
                                getUserTable();
                                Commentcounts()//不同按钮赋值
                                historyComment();//不同历史评论赋值
                            }
                        }else {
                            layer.msg('删除失败')
                        }
                    }
                })
            })
    }

    function getTime(){
        var date=new Date();
        var year=date.getFullYear();
        var month=date.getMonth()+1;
        var day=date.getDate();
        var hour=date.getHours();
        var minute=date.getMinutes();
        var second=date.getSeconds();
        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
        today = dateStr;
    }
    function zeroFilling(data){
        if(data<10){
            data="0"+data;
        }
        return data;
    }

})